/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;


import java.util.ArrayList;
import java.util.List;

import Hibernate.HibernateUtil;
import pojo.Website;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class WebsiteDAO extends AbstractDAO {
	
	@Override
	protected Class getPOJOClass() {
		
		return Website.class;
	}
    public Website getWebsiteByID(int id){
        Website website = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            website = (Website)session.get(Website.class, id);
        }
        catch(HibernateException ex){
            
        }
        finally{
            session.close();
        }
        return website;
    }
    
    

    public void insert(String webname, String menu1,String menu2, String type)
    {
    	WebsiteDAO dao= new WebsiteDAO();
    	ArrayList<Website> list =dao.getList();
    	int id;
    	if(list.size()>0)
    	{	
    		Website web= list.get(list.size()-1);
    		 id= web.getWebsiteId()+1;
    	}
    	else
    	{
    		id=1;
    	}
    
    	Website wb= new Website(webname,menu1,menu2,type, type, type,null,null,null, null, null);
    	wb.setWebsiteId(id);
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.save(wb);
			session.flush();
			transaction.commit();
			
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
			System.err.println(ex);
		}
		finally {
			session.close();
		}
    }
    
    public void insert2(String webname, String menu1,String menu2, String type)
    {
    	WebsiteDAO dao= new WebsiteDAO();
    	Website wb= new Website(webname,menu1,menu2,type, type, type, null,null,null, null, null);

    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.save(wb);
			session.flush();
			transaction.commit();
			
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
			System.err.println(ex);
		}
		finally {
			session.close();
		}
    }
    
    public boolean insert(Website web)
    {
    	boolean flag= false;
    	WebsiteDAO dao= new WebsiteDAO();
    	List<Website> l= dao.getList();
    	for(int i= 0; i<l.size();i++)
    	{
    		Website t = l.get(i);
    		if(t.getWebsite().equals(web.getWebsite()))
    			return false;
    	}
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			session.save(web);
			session.flush();
			transaction.commit();
			flag= true;
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
			System.err.println(ex);
		}
		finally {
			session.close();
		}
		
		return flag;
    }

    
    public void UpdateOrInsert(Website website){
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.saveOrUpdate(website);
			transaction.commit();
			
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
		}
		finally {
			session.close();
		}
    }
    
    public boolean delete(Website w)
    {
    	WebsiteDAO webdao= new WebsiteDAO();
    	Website web= webdao.getWebsiteByID(w.getWebsiteId());
    	boolean flag= false;
    	if(web==null) return flag;
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			
			String sql = "select link from Link link where link.website.websiteId = :ID"
					;
			Query query = (Query) session.createQuery(sql).setInteger("ID", web.getWebsiteId());


		
			session.delete(web);
			session.flush();
			transaction.commit();
			flag = true;
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
			System.err.println(ex);
		}
		finally {
			session.close();
		}
		
		return flag;
    }
    
    public boolean update(Website web)
    {
    	boolean flag= false;
    	Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		
		try {
			transaction = session.beginTransaction();
			session.update(web);
			session.flush();
			transaction.commit();
			flag= true;
		} catch (HibernateException ex) {
			// Log the exception
			transaction.rollback();
			System.err.println(ex);
		}
		finally {
			session.close();
		}
		
		return flag;
    }
}
